<?php
/**
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file license.txt or that can be found at 
 * http://code.google.com/p/billboardce/wiki/License.
 *
 * @package    Slide_IndexController
 * @category   Controller
 * @license    http://code.google.com/p/billboardce/wiki/License New BSD License
 * @version    SVN: $Id: $
 */

/**
 * Main controller for the application
 *
 * @package    Slide_IndexController
 * @category   Controller
 */
class Slide_IndexController extends Zend_Controller_Action 
{   
          
    /**
     * Shows the slides that the user has created
     *
     */
    public function indexAction()
    {
        // @todo right now this lists all slides.  Should just list the ones the user has created
        
        $this->view->acl = array(
                               'details' => $this->_helper->hasAccess('details'),
                               'add'     => $this->_helper->hasAccess('add'),
                               'edit'    => $this->_helper->hasAccess('edit'),
                               'delete'  => $this->_helper->hasAccess('delete')
                           );
        
        $slide = new Slide();
        
        $this->view->slides = $slide->fetchAll(null, 'title');
        
        $this->_helper->pageTitle('slide-index-index:title');
    }
    
    /**
     * Returns the slide info to the slidestream editor
     */
    public function getSlideInfoAction()
    {
        
        $get = Zend_Registry::get('getFilter');
        
        if (!isset($get->slideId)) {
            throw new Ot_Exception_Input('No slide ID provided');
        }
        
        $slide = new Slide();
        
        $thisSlide = $slide->find($get->slideId);
        
        if (is_null($thisSlide)) {
            throw new Ot_Exception_Input('Slide not found');
        }
        
        $this->_helper->viewRenderer->setNeverRender();
        $this->_helper->layout->disableLayout();
        
        echo Zend_Json::encode($thisSlide->toArray());
    }
    
    /**
     * Lets the user create a new slide.  If this is in an AJAX context, then 
     * we'll expect a slide stream ID to be given that the slide should also be
     * added to and we'll disable the layout.
     */
    public function addAction()
    {
        $slide = new Slide();
        
        $messages = array();
        
        $form = $slide->form();
        
        $get = Zend_Registry::get('getFilter');
        
        if ($this->_request->isXmlHttpRequest()) {

            if (!isset($get->slideStreamId) && !empty($get->slideStreamId)) {
                throw new Ot_Exception_Input('No slide stream ID given, but one was expected');
            }
            
            $form->setAction($this->view->baseUrl() . '/slide/index/add');
            
            $slideStreamId = $form->createElement('hidden', 'slideStreamId');
            $slideStreamId->setValue($get->slideStreamId);
            $slideStreamId->setDecorators(array(
                array('ViewHelper', array('helper' => 'formHidden'))
            ));

            $form->addElement($slideStreamId);
            
            $this->_helper->layout()->disableLayout();
        }       
        
        if ($this->_request->isPost()) {
                
            if ($form->isValid($_POST)) {
                
                $data = array(
                            'title' => $form->getValue('title'),
                            'url'   => $form->getValue('url')
                        );
                
                $slideId = $slide->insert($data);
                
                if (!is_null($form->getValue('slideStreamId'))) {
                    $slideStreamMap = new Slide_Stream_Map();
                    
                    $data = array(
                                'slideStreamId' => $form->getValue('slideStreamId'),
                                'entityType'    => 'slide',
                                'entityTypeId'  => $slideId,
                                'order'         => 1
                            );

                    $slideStreamMap->insert($data);
                    
                    $this->_helper->viewRenderer->setNeverRender();
                    echo $slideId;
                    
                } else {
                
                    $this->_helper->flashMessenger->addMessage('Slide created successfully!');
                    $this->_helper->redirector->gotoUrl('/slide/index');
                }
            } else {
                $messages[] = "There was a problem submitting your form";
            }
        }
        
        $this->view->messages = $messages;
        $this->view->form = $form; 
        $this->_helper->pageTitle('slide-index-add:title');
    }
}